package com.ionehe.group.common.uaa.ex.domain.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ionehe.group.common.io.Response;
import com.ionehe.group.common.uaa.ex.model.UaaSetMeal;
import com.ionehe.group.common.uaa.ex.model.UaaSetMealFunction;
import com.ionehe.group.common.uaa.ex.dto.Paging;
import com.ionehe.group.common.uaa.ex.dto.SetMealDTO;
import com.ionehe.group.common.uaa.ex.dto.SetMealListNotPageDTO;
import com.ionehe.group.common.uaa.ex.dao.SetMealDao;
import com.ionehe.group.common.uaa.ex.dao.SetMealFunctionDao;
import com.ionehe.group.common.uaa.ex.domain.service.SetMealReadService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Copyright (c) 2020 ionehe.com
 * Date: 2020/11/25 0025
 * Time: 10:03
 *
 * @author 2020年 <a href="mailto:a@ionehe.com">秀</a>
 */
@Service
@Slf4j
public class SetMealReadServiceImpl implements SetMealReadService {
    @Autowired
    SetMealDao setMealDao;
    @Autowired
    SetMealFunctionDao setMealFunctionDao;

    @Override
    public Response<Paging<SetMealDTO>> setMealList(Integer pageNum, Integer pageSize) {
        Page<SetMealDTO> page = PageHelper.startPage(pageNum, pageSize);
//        查询所有未删除的套餐
        log.info("SetMealServiceReadeImpl[]setMealList[]start!");
        List<UaaSetMeal> setMealList = setMealDao.selectList();
        if (setMealList == null) {
            Response.no("套餐为空");
        }
//        组装返回的类
        List<SetMealDTO> setMealDTOList=new ArrayList<>();
        for (UaaSetMeal setMeal : setMealList) {
//        根据idlist查询套餐权限,组装functionid
        ArrayList<Long> functionIds = new ArrayList<>();
                Example example = new Example(UaaSetMealFunction.class);
                example.createCriteria().andEqualTo("setMealId", setMeal.getId());
                List<UaaSetMealFunction> setMealFunctions = setMealFunctionDao.selectByExample(example);
                for (UaaSetMealFunction setMealFunction : setMealFunctions) {
                    functionIds.add(setMealFunction.getFunctionId());
                }
                    SetMealDTO setMealDTO = new SetMealDTO();
                    setMealDTO.setId(setMeal.getId());
                    setMealDTO.setDuration(setMeal.getDuration());
                    setMealDTO.setName(setMeal.getName());
                    setMealDTO.setFunctionIdList(functionIds);
                    setMealDTOList.add(setMealDTO);
                }
        log.info("SetMealServiceReadeImpl[]setMealList[]success!");
        Paging<SetMealDTO> paging = new Paging<>(page.getTotal(), setMealDTOList);
        return Response.yes(paging);
    }

    @Override
    public Response<List<SetMealListNotPageDTO>> setMealListNotPage() {
        Example e = new Example(UaaSetMeal.class);
        e.createCriteria()
                .andEqualTo("isDelete", false);
        List<UaaSetMeal> setMealList = setMealDao.selectByExample(e);
        List<SetMealListNotPageDTO> listNotPageDTOS=new ArrayList<>();
        for (UaaSetMeal setMeal : setMealList) {
            SetMealListNotPageDTO setMealListNotPageDTO=new SetMealListNotPageDTO();
            setMealListNotPageDTO.setName(setMeal.getName());
            setMealListNotPageDTO.setId(setMeal.getId());
            Date startTime = setMeal.getCreateTime();
            // 计算超时时间
            Calendar calendar = Calendar.getInstance();
            if (startTime != null) {
                calendar.setTime(startTime);
            }
            setMealListNotPageDTO.setDuration(setMeal.getDuration());
            listNotPageDTOS.add(setMealListNotPageDTO);
        }

        return Response.yes(listNotPageDTOS);
    }
}
